home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / oldwish / wishMain.c < prev    next >
C/C++ Source or Header  |  1990-02-26  |  20KB  |  721 lines

  1. /* 
  2.  * wishMain.c --
  3.  *
  4.  *    Main routine for wish stuff.
  5.  *
  6.  * Copyright 1987 Regents of the University of California
  7.  * All rights reserved.
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose and without
  10.  * fee is hereby granted, provided that the above copyright
  11.  * notice appear in all copies.  The University of California
  12.  * makes no representations about the suitability of this
  13.  * software for any purpose.  It is provided "as is" without
  14.  * express or implied warranty.
  15.  */
  16.  
  17. #ifndef lint
  18. static char rcsid[] = "$Header: /a/newcmds/wish/RCS/wishMain.c,v 1.4 89/01/18 00:07:38 mgbaker Exp Locker: mgbaker $ SPRITE (Berkeley)";
  19. #endif not lint
  20.  
  21. #include <X11/Xlib.h>
  22. #include <X11/Xutil.h>
  23. #define    Time    SpriteTime
  24. #include <sys/time.h>
  25. #include "string.h"
  26. #include <fs.h>
  27. #include "sx.h"
  28. #include "util.h"
  29. #include "monitorClient.h"
  30. #include "wishInt.h"
  31. #include <signal.h>
  32.  
  33. int    size;
  34.  
  35. /*
  36.  * The global table in which pointers to window information for each
  37.  * wish window is kept.
  38.  */
  39. XContext    wishWindowContext;
  40. XContext    wishGroupWindowContext;
  41.  
  42. /* Whether debugging is turned on or not */
  43. Boolean        wishDebugP = FALSE;
  44.  
  45. /* Whether the application should pick the initial window size */
  46. Boolean        wishPickSizeP = TRUE;
  47. /* whether the application can resize the window */
  48. Boolean        wishResizeP = FALSE;
  49. /* whether to display headers for groups with no files. */
  50. Boolean        wishShowEmptyGroupsP = FALSE;
  51. /* The display */
  52. Display        *wishDisplay;
  53.  
  54. /*
  55.  * The application name.
  56.  */
  57. char    *wishApplication = "wish";
  58.  
  59. /*
  60.  * For formulating error strings.  How big is big enough?
  61.  */
  62. char    wishErrorMsg[2 * MAXPATHLEN + 50];
  63.  
  64. /* Display info for calculating max size of windows */
  65. int        wishRootHeight;
  66. int        wishRootWidth;
  67.  
  68. /* Keep track of current directory. */
  69. char    wishCurrentDirectory[MAXPATHLEN + 1];
  70.  
  71. /*
  72.  * Wish's startup directory in which to look for the default .files if there
  73.  * aren't others.
  74.  */
  75. char    wishStartUp[MAXPATHLEN];
  76.  
  77. #ifdef ICON
  78. /*
  79.  * Icon window information.
  80.  */
  81.  
  82. #define ICONDIR    "/usr2/icon/X"
  83.  
  84. typedef struct {
  85.     int x;            /* Initial X, Y positions. */
  86.     int y;            
  87.     int width;
  88.     int height;
  89.     int foreground;        /* Foreground, background colors 
  90.                  * are same as the main window. */
  91.     int background;
  92.     char *name;            /* Filename of the icon bitmap file. */
  93.     Pixmap bitmap;        /* Bitmap data. */
  94.     Window window;        /* ID of the icon window. */
  95. } TxIconInfo;
  96. static TxIconInfo iconInfo = { 0, 0, 0, 0, 0, 0, NULL, NULL,};
  97. static void CreateIcon();
  98. #endif /* ICON */
  99.  
  100.  
  101. /*
  102.  *----------------------------------------------------------------------
  103.  *
  104.  * main --
  105.  *
  106.  *    Main routine for wish.
  107.  *
  108.  * Results:
  109.  *    None.
  110.  *
  111.  * Side effects:
  112.  *    Many.
  113.  *
  114.  *----------------------------------------------------------------------
  115.  */
  116. main(argc, argv)
  117.     int    argc;
  118.     char    *argv[];
  119. {
  120.     char    *string;
  121.     char    *geometry = NULL;    /* for window set-up */
  122.     char    *font;
  123.     WishWindow    *aWindow = NULL;
  124.     char    *startDir = NULL;
  125. #ifdef NOTDEF
  126.     struct    timeval    alarm;
  127. #else /* NOTDEF */
  128.     SpriteTime    alarm;
  129. #endif /* NOTDEF */
  130.     char    *cPtr;
  131.     extern    void    HandleMonitorEvent();
  132.     extern    void    WishDisplayEventProc();
  133.     extern    void    HandleMonitorStats();
  134.  
  135.     /* get directory in which we're starting up */
  136.     (void) Whence(wishStartUp, argv[0]);
  137.  
  138.     /*
  139.      * Process command line arguments.
  140.      */
  141.     while (argc > 1 ) { 
  142.     argc--;
  143.     argv++;
  144.     if (strcmp(argv[0], "-debug") == 0) {
  145.         wishDebugP = TRUE;
  146.         continue;
  147.     }
  148.     if (strcmp(argv[0], "-nopick") == 0) {
  149.         wishPickSizeP = FALSE;
  150.         continue;
  151.     }
  152.     if (strcmp(argv[0], "-resize") == 0) {
  153.         wishResizeP = TRUE;
  154.         continue;
  155.     }
  156.     if (strcmp(argv[0], "-empty") == 0) {
  157.         wishShowEmptyGroupsP = TRUE;
  158.         continue;
  159.     }
  160.     if (strcmp(argv[0], "-usage") == 0 ||
  161.         strcmp(argv[0], "-u") == 0) {
  162.         fprintf(stderr, "%s\n\n%s\n%s\n%s\n%s\n",
  163.             "wish -debug -nopick -resize -empty -usage dirname",
  164.             "-debug    Print debug messages and don't fork.",
  165.             "-nopick    Don't automatically pick initial window size.",
  166.             "-resize    Allow automatic resizing of window.",
  167.             "-empty    Display headers for groups without entries.");
  168.         exit(1);
  169.     }
  170.     startDir = argv[0];
  171.     }
  172.  
  173.     /* Don't fork into background if debugging is on - so dbx works, etc. */
  174.     if (!wishDebugP) {
  175.     setpgrp(0,getpid());
  176.     Proc_Detach(0);
  177.     }
  178.     /*
  179.      * Initialize the display and wishWindowContext and
  180.      * wishGroupWindowContext.
  181.      */
  182.     if ((wishDisplay = XOpenDisplay(NULL)) == NULL) {
  183.     fprintf(stderr, "Could not open display.\n");
  184.     exit(1);
  185.     }
  186.     if (wishDebugP) {
  187.     XSynchronize(wishDisplay, True);
  188.     }
  189.     Sx_SetErrorHandler();
  190.  
  191.     wishRootHeight = DisplayHeight(wishDisplay,
  192.         DefaultScreen(wishDisplay));
  193.     wishRootWidth = DisplayWidth(wishDisplay,
  194.         DefaultScreen(wishDisplay));
  195.  
  196.     /*
  197.      * 8 should be more than enough considering there's only 1 window
  198.      * right now!
  199.      */
  200.     wishWindowContext = XUniqueContext();
  201.     wishGroupWindowContext = XUniqueContext();
  202.  
  203.     /*
  204.      * Create a dummy structure for passing to WishCreate to create
  205.      * the first window and then process XDefault arguments.  
  206.      */
  207.     aWindow = (WishWindow *) malloc(sizeof (WishWindow));
  208.     
  209.     if (startDir != NULL) {
  210.     if (Util_CanonicalDir(startDir, NULL, aWindow->dir) == NULL) {
  211.         /* error message returned in aWindow->dir */
  212.         Sx_Panic(wishDisplay, aWindow->dir);
  213.     }
  214.     } else if (getwd(aWindow->dir) == NULL) {
  215.     sprintf(wishErrorMsg,
  216.         "%s: Couldn't get current working directory.\n",
  217.         wishApplication);
  218.     Sx_Panic(wishDisplay, wishErrorMsg);
  219.     }
  220.  
  221.     if ((string = XGetDefault(wishDisplay, wishApplication,
  222.         "background")) != NULL) {
  223.     if ((aWindow->background =
  224.         Util_StringToColor(wishDisplay, string)) == -1) {
  225.         aWindow->background = BlackPixel(wishDisplay,
  226.             DefaultScreen(wishDisplay));
  227.     }
  228. else {
  229. }
  230.     
  231.     } else {
  232.     aWindow->background = BlackPixel(wishDisplay,
  233.         DefaultScreen(wishDisplay));
  234.     }
  235.  
  236.     if ((string = XGetDefault(wishDisplay, wishApplication,
  237.         "foreground")) != NULL) {
  238.     if ((aWindow->foreground =
  239.         Util_StringToColor(wishDisplay, string)) == -1) {
  240.         /* Try to make it the opposite of the background */
  241.         if (aWindow->background == WhitePixel(wishDisplay,
  242.             DefaultScreen(wishDisplay))) {
  243.         aWindow->foreground = BlackPixel(wishDisplay,
  244.             DefaultScreen(wishDisplay));
  245.         } else {
  246.         aWindow->foreground = WhitePixel(wishDisplay,
  247.             DefaultScreen(wishDisplay));
  248.         }
  249.     }    
  250.     /* At least try to make it the opposite of the background */
  251.     } else if (aWindow->background == WhitePixel(wishDisplay,
  252.         DefaultScreen(wishDisplay))) {
  253.     aWindow->foreground = BlackPixel(wishDisplay,
  254.         DefaultScreen(wishDisplay));
  255.     } else {
  256.     aWindow->foreground = WhitePixel(wishDisplay,
  257.         DefaultScreen(wishDisplay));
  258.     }
  259.  
  260.     if ((string = XGetDefault(wishDisplay, wishApplication, "border"))
  261.         != NULL) {
  262.     if ((aWindow->border =
  263.         Util_StringToColor(wishDisplay, string)) == -1) {
  264.         /* Try to make it the opposite of the background */
  265.         if (aWindow->background == WhitePixel(wishDisplay,
  266.             DefaultScreen(wishDisplay))) {
  267.         aWindow->border = BlackPixel(wishDisplay,
  268.             DefaultScreen(wishDisplay));
  269.         } else {
  270.         aWindow->border = WhitePixel(wishDisplay,
  271.             DefaultScreen(wishDisplay));
  272.         }
  273.     }
  274.     /* At least try to make it the opposite of the background */
  275.     } else if (aWindow->background == WhitePixel(wishDisplay,
  276.         DefaultScreen(wishDisplay))) {
  277.     aWindow->border = BlackPixel(wishDisplay,
  278.         DefaultScreen(wishDisplay));
  279.     } else {
  280.     aWindow->border = WhitePixel(wishDisplay,
  281.         DefaultScreen(wishDisplay));
  282.     }
  283.  
  284.     if ((string = XGetDefault(wishDisplay, wishApplication, "selection"))
  285.         != NULL) {
  286.     if ((aWindow->selection =
  287.         Util_StringToColor(wishDisplay, string)) == -1) {
  288.         /* Try to make it the opposite of the background */
  289.         if (aWindow->background == WhitePixel(wishDisplay,
  290.             DefaultScreen(wishDisplay))) {
  291.         aWindow->selection = BlackPixel(wishDisplay,
  292.             DefaultScreen(wishDisplay));
  293.         } else {
  294.         aWindow->selection = WhitePixel(wishDisplay,
  295.             DefaultScreen(wishDisplay));
  296.         }
  297.     }    
  298.     /* At least try to make it the opposite of the background */
  299.     } else if (aWindow->background == WhitePixel(wishDisplay,
  300.         DefaultScreen(wishDisplay))) {
  301.     aWindow->selection = BlackPixel(wishDisplay,
  302.         DefaultScreen(wishDisplay));
  303.     } else {
  304.     aWindow->selection = WhitePixel(wishDisplay,
  305.         DefaultScreen(wishDisplay));
  306.     }
  307.  
  308.     if ((string = XGetDefault(wishDisplay, wishApplication,
  309.         "titlebackground")) != NULL) {
  310.     if ((aWindow->titleBackground =
  311.         Util_StringToColor(wishDisplay, string)) == -1) {
  312.         aWindow->titleBackground = aWindow->background;
  313.     }
  314.     } else {
  315.     aWindow->titleBackground = aWindow->background;
  316.     }
  317.  
  318.     if ((string = XGetDefault(wishDisplay, wishApplication,
  319.         "txbackground")) != NULL) {
  320.     if ((aWindow->txBackground =
  321.         Util_StringToColor(wishDisplay, string)) == -1) {
  322.         aWindow->txBackground = aWindow->background;
  323.     }
  324.     } else {
  325.     aWindow->txBackground = aWindow->background;
  326.     }
  327.     if ((string = XGetDefault(wishDisplay, wishApplication,
  328.         "menubackground")) != NULL) {
  329.     if ((aWindow->menuBackground =
  330.         Util_StringToColor(wishDisplay, string)) == -1) {
  331.         aWindow->menuBackground = aWindow->background;
  332.     }
  333.     } else {
  334.     aWindow->menuBackground = aWindow->background;
  335.     }
  336.     if ((string = XGetDefault(wishDisplay, wishApplication,
  337.         "sortbackground")) != NULL) {
  338.     if ((aWindow->sortBackground =
  339.         Util_StringToColor(wishDisplay, string)) == -1) {
  340.         aWindow->sortBackground = aWindow->background;
  341.     }
  342.     } else {
  343.     aWindow->sortBackground = aWindow->background;
  344.     }
  345.     if ((string = XGetDefault(wishDisplay, wishApplication,
  346.         "fieldsbackground")) != NULL) {
  347.     if ((aWindow->fieldsBackground =
  348.         Util_StringToColor(wishDisplay, string)) == -1) {
  349.         aWindow->fieldsBackground = aWindow->background;
  350.     }
  351.     } else {
  352.     aWindow->fieldsBackground = aWindow->background;
  353.     }
  354.  
  355.     if ((string = XGetDefault(wishDisplay, wishApplication,
  356.         "entrybackground")) != NULL) {
  357.     if ((aWindow->entryBackground =
  358.         Util_StringToColor(wishDisplay, string)) == -1) {
  359.         aWindow->entryBackground = aWindow->background;
  360.     }
  361.     } else {
  362.     aWindow->entryBackground = aWindow->background;
  363.     }
  364.  
  365.     if ((string = XGetDefault(wishDisplay, wishApplication,
  366.         "scrollbackground")) != NULL) {
  367.     if ((aWindow->scrollBackground =
  368.         Util_StringToColor(wishDisplay, string)) == -1) {
  369.         aWindow->scrollBackground = aWindow->background;
  370.     }
  371.     } else {
  372.     aWindow->scrollBackground = aWindow->background;
  373.     }
  374.  
  375.     if ((string = XGetDefault(wishDisplay, wishApplication,
  376.         "titleforeground")) != NULL) {
  377.     if ((aWindow->titleForeground =
  378.         Util_StringToColor(wishDisplay, string)) == -1) {
  379.         aWindow->titleForeground = aWindow->foreground;
  380.     }    
  381.     } else {
  382.     aWindow->titleForeground = aWindow->foreground;
  383.     }
  384.  
  385.     if ((string = XGetDefault(wishDisplay, wishApplication,
  386.         "txforeground")) != NULL) {
  387.     if ((aWindow->txForeground =
  388.         Util_StringToColor(wishDisplay, string)) == -1) {
  389.         aWindow->txForeground = aWindow->foreground;
  390.     }    
  391.     } else {
  392.     aWindow->txForeground = aWindow->foreground;
  393.     }
  394.  
  395.     if ((string = XGetDefault(wishDisplay, wishApplication,
  396.         "menuforeground")) != NULL) {
  397.     if ((aWindow->menuForeground =
  398.         Util_StringToColor(wishDisplay, string)) == -1) {
  399.         aWindow->menuForeground = aWindow->foreground;
  400.     }
  401.     } else {
  402.     aWindow->menuForeground = aWindow->foreground;
  403.     }
  404.  
  405.     if ((string = XGetDefault(wishDisplay, wishApplication,
  406.         "sortforeground")) != NULL) {
  407.     if ((aWindow->sortForeground =
  408.         Util_StringToColor(wishDisplay, string)) == -1) {
  409.         aWindow->sortForeground = aWindow->foreground;
  410.     }    
  411.     } else {
  412.     aWindow->sortForeground = aWindow->foreground;
  413.     }
  414.  
  415.     if ((string = XGetDefault(wishDisplay, wishApplication,
  416.         "fieldsforeground")) != NULL) {
  417.     if ((aWindow->fieldsForeground =
  418.         Util_StringToColor(wishDisplay, string)) == -1) {
  419.         aWindow->fieldsForeground = aWindow->foreground;
  420.     }    
  421.     } else {
  422.     aWindow->fieldsForeground = aWindow->foreground;
  423.     }
  424.  
  425.     if ((string = XGetDefault(wishDisplay, wishApplication,
  426.         "entryforeground")) != NULL) {
  427.     if ((aWindow->entryForeground =
  428.         Util_StringToColor(wishDisplay, string)) == -1) {
  429.         aWindow->entryForeground = aWindow->foreground;
  430.     }    
  431.     } else {
  432.     aWindow->entryForeground = aWindow->foreground;
  433.     }
  434.  
  435.     if ((string = XGetDefault(wishDisplay, wishApplication,
  436.         "scrollforeground")) != NULL) {
  437.     if ((aWindow->scrollForeground =
  438.         Util_StringToColor(wishDisplay, string)) == -1) {
  439.         aWindow->scrollForeground = aWindow->foreground;
  440.     }    
  441.     } else {
  442.     aWindow->scrollForeground = aWindow->foreground;
  443.     }
  444.  
  445.     if ((string = XGetDefault(wishDisplay, wishApplication,
  446.         "borderwidth")) != NULL) {
  447.     if ((aWindow->borderWidth = strtol(string, &cPtr, 10)) == 0 &&
  448.         cPtr == string) {
  449.         /* AtoI failed */
  450.         aWindow->borderWidth = 2;
  451.     }
  452.     } else {
  453.     aWindow->borderWidth = 2;
  454.     }
  455.  
  456.     if ((string = XGetDefault(wishDisplay, wishApplication,
  457.         "titleborder")) != NULL) {
  458.     if ((aWindow->titleBorder =
  459.         Util_StringToColor(wishDisplay, string)) == -1) {
  460.         aWindow->titleBorder = aWindow->border;
  461.     }
  462.     } else {
  463.     aWindow->titleBorder = aWindow->border;
  464.     }
  465.  
  466.     if ((string = XGetDefault(wishDisplay, wishApplication, "txborder"))
  467.         != NULL) {
  468.     if ((aWindow->txBorder =
  469.         Util_StringToColor(wishDisplay, string)) == -1) {
  470.         aWindow->txBorder = aWindow->border;
  471.     }
  472.     } else {
  473.     aWindow->txBorder = aWindow->border;
  474.     }
  475.  
  476.     if ((string = XGetDefault(wishDisplay, wishApplication,
  477.         "scrollelevator")) != NULL) {
  478.     if ((aWindow->scrollElevator =
  479.         Util_StringToColor(wishDisplay, string)) == -1) {
  480.         aWindow->scrollElevator = aWindow->scrollForeground;
  481.     }    
  482.     } else {
  483.     aWindow->scrollElevator = aWindow->scrollForeground;
  484.     }
  485.  
  486.     if ((geometry = XGetDefault(wishDisplay, wishApplication, "geometry"))
  487.         == NULL) {
  488.     geometry = "=550x350+160+160";
  489.     }
  490.     aWindow->geometry = geometry;
  491.  
  492.     if ((font = XGetDefault(wishDisplay, wishApplication, "font"))
  493.         != NULL) {
  494.     aWindow->fontPtr = XLoadQueryFont(wishDisplay, font);
  495.     if (aWindow->fontPtr != NULL) {
  496.         Sx_SetDefaultFont(aWindow->fontPtr);
  497.     } else {
  498.         aWindow->fontPtr = Sx_GetDefaultFont(wishDisplay);
  499.     }
  500.     } else {
  501.     aWindow->fontPtr = Sx_GetDefaultFont(wishDisplay);
  502.     }
  503.  
  504.     if ((font = XGetDefault(wishDisplay, wishApplication, "titlefont"))
  505.         != NULL) {
  506.     aWindow->titleFontPtr = XLoadQueryFont(wishDisplay, font);
  507.     if (aWindow->titleFontPtr != NULL) {
  508.         Sx_SetDefaultFont(aWindow->titleFontPtr);
  509.     }
  510.     }
  511.     if (aWindow->titleFontPtr == NULL) {
  512.     aWindow->titleFontPtr = Sx_GetDefaultFont(wishDisplay);
  513.     }
  514.  
  515. /* NO!  I need an interpreter here for calls to Tx_SetupAndFork, etc. */
  516.     aWindow->interp = NULL;
  517.  
  518.     if (!MonClient_Register()) {
  519.     Sx_Panic(wishDisplay,
  520.         "Initialization of file system monitor failed.");
  521.     }
  522.  
  523.     /* create the first window */
  524.     if (startDir != NULL) {
  525.     if (WishCreate(aWindow, startDir) == NULL) {
  526.         Sx_Panic(wishDisplay, "Couldn't create first display window.");
  527.     }
  528.     } else {
  529.     if (WishCreate(aWindow, NULL) == NULL) {
  530.         Sx_Panic(wishDisplay, "Couldn't create first display window.");
  531.     }
  532.     }
  533.     free(aWindow);        /* free dummy structure */
  534.  
  535. #ifdef ICON
  536.     /*
  537.      * Create the icon for the window. This must done after the
  538.      * main window is initialized.
  539.      */
  540.  
  541.     CreateIcon(display, window, info.foreground, info.background);
  542. #endif /* ICON */
  543.  
  544.     alarm.seconds = 5;
  545.     alarm.microseconds = 0;
  546.     Fs_EventHandlerCreate(monClient_ReadPort, FS_READABLE,
  547.         HandleMonitorEvent, NULL);
  548.     Fs_EventHandlerCreate(ConnectionNumber(wishDisplay), FS_READABLE,
  549.         WishDisplayEventProc, NULL);
  550.     Fs_TimeoutHandlerCreate(alarm, TRUE, HandleMonitorStats,
  551.         (ClientData) NULL);
  552.     while (wishWindowCount > 0) {
  553.     while ((size = QLength(wishDisplay)) > 0) {
  554.         XEvent    event;
  555.  
  556.         XNextEvent(wishDisplay, &event);
  557.         Sx_HandleEvent(&event);
  558.     }
  559.     /* No longer needed? */
  560. #ifdef NOTDEF
  561.     Tx_FlushStreams();
  562. #endif NOTDEF
  563.     Tx_Update();
  564.     Mx_Update();
  565.     if ((size = QLength(wishDisplay)) > 0) {
  566.         continue;
  567.     }
  568.     XFlush(wishDisplay);
  569.     Fs_Dispatch();
  570.     }
  571.  
  572. # ifdef NOTDEF
  573. /*
  574.  * Can't do this yet.  I don't know what I'll do about the utmp
  575.  * stuff for now.
  576.  */
  577.     if (txRegisterPty) {
  578.     char    *ptyName;
  579.    
  580.     if ((ptyName = TxWindowToPtyName(window)) != NULL) {
  581.         (void) TxUtmpEntry(FALSE, ptyName, (char *) NULL);
  582.     }
  583.     }
  584. # endif /* NOTDEF */
  585.     exit(0);
  586. }
  587.  
  588.  
  589. /*ARGSUSED*/
  590. void
  591. WishDisplayEventProc(clientData, streamID, eventMask)
  592.     ClientData    clientData;
  593.     int        streamID;
  594.     int        eventMask;
  595. {
  596.     static    int    count = 0;
  597.     XEvent    event;
  598.  
  599.     do {
  600.     count++;
  601.     XNextEvent(wishDisplay, &event);
  602.     Sx_HandleEvent(&event);
  603.     } while (QLength(wishDisplay) > 0);
  604.  
  605.     return;
  606. }
  607.  
  608. /*ARGSUSED*/
  609. void
  610. HandleMonitorStats(clientData, time)
  611.     ClientData    clientData;
  612.     SpriteTime    time;
  613. {
  614.     Mon_StatDirs();
  615.     return;
  616. }
  617.  
  618. /*ARGSUSED*/
  619. void
  620. HandleMonitorEvent(clientData, streamID, eventMask)
  621.     ClientData    clientData;
  622.     int        streamID;
  623.     int        eventMask;
  624. {
  625.     WishHandleMonitorUpdates();
  626.     return;
  627. }
  628.  
  629. #ifdef ICON
  630.  
  631. /*
  632.  *----------------------------------------------------------------------
  633.  *
  634.  * CreateIcon --
  635.  *
  636.  *    This procedure creates an icon window for the main window 
  637.  *    using data read in from a X bitmap file.
  638.  *
  639.  *    If the icon file name is "localhost", the hostname of the machine
  640.  *    is used to locate an icon file for the host in a standard directory.
  641.  *
  642.  * Results:
  643.  *    None.
  644.  *
  645.  * Side effects:
  646.  *    An icon window is created.
  647.  *
  648.  *----------------------------------------------------------------------
  649.  */
  650.  
  651. static void
  652. CreateIcon(display, window, foreground, background)
  653.     Display    *display;
  654.     Window window;        /* An icon will be created for this window. */
  655.     unsigned long foreground;    /* Foreground color of the icon. */
  656.     unsigned long background;    /* Background color of the icon. */
  657. {
  658.     char iconfile[256];        /* Buffer to hold a complete icon file name. */
  659.     char hostname[100];        /* Buffer to hold the hostname. */
  660.     char *dot;
  661.     int status;
  662.     XWMHints hints;
  663.  
  664.     /*
  665.      * See if a icon file name was given on the command line or is in the
  666.      * .Xdefaults file.
  667.      */
  668.     if (iconInfo.name == (char *) NULL) {
  669.     iconInfo.name = XGetDefault(display, "tx", "icon");
  670.     if (iconInfo.name == (char *) NULL) {
  671.         return;
  672.     }
  673.     }
  674.  
  675.     /*
  676.      * If the file name is localhost, look in a standard directory for
  677.      * a file named after the host.
  678.      */
  679.     if (strcmp(iconInfo.name, "localhost") == 0) {
  680.     gethostname(hostname, sizeof(hostname));
  681.     dot = strchr(hostname, '.');
  682.     if (dot != (char *) NULL) {
  683.         *dot = '\0';
  684.     }
  685.     sprintf(iconfile, "%s/%s", ICONDIR, hostname);
  686.     iconInfo.name = iconfile;
  687.     }
  688.  
  689.     status = XReadBitmapFile(display, window, iconInfo.name, &iconInfo.width,
  690.         &iconInfo.height, &iconInfo.bitmap, 0, 0);
  691.  
  692.     if (status == 0) {
  693.     fprintf(stderr,
  694.         "Tx: can't open icon file %s, using default icon.\n", 
  695.         iconInfo.name);
  696.     return;
  697.     } else if (status < 0) {
  698.     fprintf(stderr, 
  699.         "Tx: icon file %s has invalid format.\n", iconInfo.name);
  700.     return;
  701.     }
  702.  
  703.     hints.flags = InputHint|StateHint|IconWindowHint
  704.         |IconPixmapHint|IconPositionHint;
  705.     hints.input = False;
  706.     hints.icon_pixmap = iconInfo.bitmap;
  707.     hints.icon_window = XCreateSimpleWindow(display,
  708.         RootWindow(display, DefaultScreen(display)),
  709.         iconInfo.x, iconInfo.y, iconInfo.width, iconInfo.height,
  710.         0, foreground, background);
  711.     hints.icon_x = iconInfo.x;
  712.     hints.icon_y = iconInfo.y;
  713.     XSetWMHints(display, window, &hints);
  714.  
  715. #ifdef notdef    /* Shouldn't be needed under X11. */
  716.     (void) Sx_HandlerCreate(display, iconInfo.window, ExposureMask,
  717.         RefreshIcon, (ClientData) 0);
  718. #endif
  719. }
  720. #endif /* ICON */
  721.